<?php

namespace common\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use Helper;

class SmsLog extends ActiveRecord
{

    public $country;
    public $signid;
    public $total;

    const SCENARIO_SEND = 'send';
    const SCENARIO_VOICESEND = 'vocie_send';

    public static function tableName()
    {
        return '{{%sms_log}}';
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['addtime']
                ]
            ],

        ];
    }

    public function rules()
    {
        return [
            ['appkey', 'required', 'on' => [self::SCENARIO_SEND, self::SCENARIO_VOICESEND], 'message' => Yii::t('code', '1001')],
            ['country', 'required', 'on' => [self::SCENARIO_SEND, self::SCENARIO_VOICESEND], 'message' => Yii::t('code', '1002')],
            ['country', 'checkCountry', 'skipOnError' => false, 'skipOnEmpty' => false, 'on' => [self::SCENARIO_SEND, self::SCENARIO_VOICESEND]],
            ['data', 'required', 'on' => [self::SCENARIO_SEND, self::SCENARIO_VOICESEND], 'message' => Yii::t('code', '1003')],
            ['data', 'checkMobile', 'on' => [self::SCENARIO_SEND, self::SCENARIO_VOICESEND]],
            ['tplid', 'required', 'on' => self::SCENARIO_SEND, 'message' => Yii::t('code', '1006')],
            ['signid', 'required', 'on' => self::SCENARIO_SEND, 'message' => Yii::t('code', '1007')],
            ['content', 'required', 'on' => self::SCENARIO_SEND, 'message' => Yii::t('code', '1008')],
            ['gid', 'required', 'on' => [self::SCENARIO_SEND, self::SCENARIO_VOICESEND], 'message' => Yii::t('code', '1024')],
            ['codetype', 'default', 'value' => -1],
            ['codetype', 'string', 'length' => [1, 25], 'message' => Yii::t('code', '1009')],
            [['code', 'tplid', 'statu', 'total'], 'integer'],
            [['vparam1', 'vparam2', 'vparam3', 'ip', 'sendtype', 'content', 'uptime', 'appkey', 'data'], 'default', 'value' => null],
            [['provider', 'uptime', "mdata", 'addtime'], 'safe'],

        ];
    }

    public function checkCountry($attribute, $params)
    {
        $countrys = require(Yii::getAlias('@common/data/country.php'));
        if ((!isset($countrys[$this->country]) || empty($countrys[$this->country]))) {
            $name = array_search($this->country, array_column($countrys, 'code', 'sname'));
            if ($name === false) {
                $this->addError($attribute, Yii::t('code', '1005'));
            }
        }
    }

    public function checkMobile($attribute, $params)
    {
        if ($this->country != "CN" && $this->country != "86") {
            $flag = Helper::isMobile($this->data, true);
        } else {
            $flag = Helper::isMobile($this->data, false);
        }
        if (!$flag) {
            $this->addError($attribute, Yii::t('code', '1004'));
        }
    }

    /**
     * 更新最新短信发送时间缓存
     * @param $appkey 用户appkey
     * @param $mobile 用户手机号码
     * @param $codetype 用户发送类型
     * @return bool 返回更新结果
     */
    public static function updateLastTimeCache($appkey, $mobile, $codetype)
    {
        $cacheName = 'smsLast_' . $appkey . "_" . $mobile . '_' . $codetype;
        Yii::$app->redis->set($cacheName, time());
        return true;
    }

    /**
     * 获取最新短信发送时间缓存
     * @param $appkey 用户appkey
     * @param $mobile 用户手机号码
     * @param $codetype 用户发送类型
     * @return int|mixed 返回结果
     */
    public static function getLastTimeCache($appkey, $mobile, $codetype)
    {
        $time = 0;
        $cacheName = 'smsLast_' . $appkey . "_" . $mobile . '_' . $codetype;
        $cache = Yii::$app->redis->get($cacheName);
        if (empty($cache)) {
            $cache = SmsLog::find()
                ->where(['appkey' => $appkey, 'data' => $mobile, 'codetype' => $codetype])
                ->orderBy('id desc')
                ->one();
        }
        if (!empty($cache)) {
            $time = isset($cache['addtime']) ? $cache['addtime'] : $cache;
        }
        return $time;
    }


}

?>